﻿using MCPlatform.Dxf.Entities;
using MCPlatform.Dxf.Readers;
using MCPlatform.Dxf.Utils;
using MCPlatform.Dxf.Views;
using MCPlatform.Models;
using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using MCPlatform.UI.Controls.Models;
using netDxf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MCPlatform.Components.Utilities
{
    public static class ProductInfo
    {
        #region Ulties




        public static (PointSettingModel, PointSettingModel, PointSettingModel, PointSettingModel, PointSettingModel) GetCuttingLengthAndWidth()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();



            PointSettingModel pXCuttingLenght = new PointSettingModel() { Parameter = productModel.CuttingLength + productModel.KnifeCompensate };
            PointSettingModel pYCuttingWidth = new PointSettingModel() { Parameter = productModel.CuttingWidth + productModel.KnifeCompensate };
            PointSettingModel nXCuttingLenght = new PointSettingModel() { Parameter = (productModel.CuttingLength + productModel.KnifeCompensate) * -1 };
            PointSettingModel nYCuttingWidth = new PointSettingModel() { Parameter = (productModel.CuttingWidth + productModel.KnifeCompensate) * -1 };

            PointSettingModel pSpace = new PointSettingModel() { Parameter = productModel.Space };

            return (pXCuttingLenght, pYCuttingWidth, nXCuttingLenght, nYCuttingWidth, pSpace);
        }


        public static (double,double) GetCuttingLengthAndWidthByDouble()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();



            PointSettingModel pXCuttingLenght = new PointSettingModel() { Parameter = productModel.CuttingLength + productModel.KnifeCompensate };
            PointSettingModel pYCuttingWidth = new PointSettingModel() { Parameter = productModel.CuttingWidth + productModel.KnifeCompensate };
            PointSettingModel nXCuttingLenght = new PointSettingModel() { Parameter = (productModel.CuttingLength + productModel.KnifeCompensate) * -1 };
            PointSettingModel nYCuttingWidth = new PointSettingModel() { Parameter = (productModel.CuttingWidth + productModel.KnifeCompensate) * -1 };

            PointSettingModel pSpace = new PointSettingModel() { Parameter = productModel.Space };

            return (productModel.CuttingLength + productModel.KnifeCompensate, productModel.CuttingWidth + productModel.KnifeCompensate);
        }

        public static int GetCuttingProductNumber()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();

            return (int)productModel.CuttingProductNumberOneTime;
        }

        public static (PointSettingModel, PointSettingModel) GetPhotoPos()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();
            PointSettingModel photoXPos = new PointSettingModel() { Parameter = productModel.XAxisPhotoPos };
            PointSettingModel photoYPos = new PointSettingModel() { Parameter = productModel.YAxisPhotoPos };


            return (photoXPos, photoYPos);
        }

        public static double GetPatternSpace()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();



            return productModel.PatternSpace;
        }


        public static double GetSpace()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();


            return productModel.Space;
        }
        public static (PointSettingModel, PointSettingModel, PointSettingModel) GetCuttingStartPos()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();
            PointSettingModel cuttingXPos = new PointSettingModel() { Parameter = productModel.StartCuttingXPos };
            PointSettingModel cuttingYPos = new PointSettingModel() { Parameter = productModel.StartCuttingYPos };
            PointSettingModel cuttingZPos = new PointSettingModel() { Parameter = productModel.StartCuttingZPos };


            return (cuttingXPos, cuttingYPos, cuttingZPos);
        }

        public static bool IsNeedPhoto()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();



            return productModel.IsPhoto;
        }

        public static bool IsNeedPunch()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();



            return productModel.IsPunch;
        }

        public static (PointSettingModel, PointSettingModel, PointSettingModel) GetPunchOffset()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);
            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();
            PointSettingModel XOffset = new PointSettingModel() { Parameter = productModel.PunchXOffset };
            PointSettingModel YOffset = new PointSettingModel() { Parameter = productModel.PunchYOffset };
            PointSettingModel ZOffset = new PointSettingModel() { Parameter = productModel.PunchZOffset };
            return (XOffset, YOffset, ZOffset);
        }
        public static (PointSettingModel, PointSettingModel, PointSettingModel) GeKnifeOffset()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);
            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();
            PointSettingModel XOffset = new PointSettingModel() { Parameter = productModel.KnifeXOffset };
            PointSettingModel YOffset = new PointSettingModel() { Parameter = productModel.KnifeYOffset };
            PointSettingModel ZOffset = new PointSettingModel() { Parameter = productModel.KnifeZOffset };
            return (XOffset, YOffset, ZOffset);
        }

        /// <summary>
        /// 查找最后一次选择的dxf文件名称
        /// </summary>
        /// <returns></returns>
        public static string? GetLastSelectFileName()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            if (productConfigs != null)
            {

                if (productConfigs.Count > 0)
                {
                    foreach (var product in productConfigs)
                    {
                        if (product.IsLastSelected)
                        {
                            return product.CADName;
                        }
                    }
                }
            }

            return null;
        }



        public static (PointSettingModel, PointSettingModel, PointSettingModel) GeUnloadStartPos()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();
            PointSettingModel XPos = new PointSettingModel() { Parameter = productModel.XAxisUnloadStartPos };
            PointSettingModel YPos = new PointSettingModel() { Parameter = productModel.YAxisUnloadStartPos };
            PointSettingModel ZPos = new PointSettingModel() { Parameter = productModel.ZAxisUnloadStartPos };


            return (XPos, YPos, ZPos);
        }


        

        

        public static int GetUnloadProductNumberOneTime()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();


            return (int)productModel.UnloadProductNumberOneTime;
        }

      

        public static int GetUnloadMaxNumber()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();

            return (int)productModel.UnloadMaxNumber;
        }



        public static double GetProductThickness()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            var productModel = productConfigs?.FirstOrDefault(config => config.IsLastSelected) ?? new ProductModel();

            return productModel.Thickness;
        }



        public static ProductModel? GetProductInfo()
        {
            var productConfigs = ReadConfigData.GetParaConfigs<ProductModel>(Paths.ProductConfigPaths);

            if (productConfigs != null)
            {
               
                if (productConfigs.Count > 0)
                {
                    foreach (var product in productConfigs)
                    {
                        if (product.IsLastSelected)
                        {
                            return product;
                        }
                    }
                }
            }

            return null;    
                    
        }






        #endregion
    }
}
